package com.hcbd.tdwt.ui.xingyu.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.util.AppUtil;
import com.hcbd.tdwt.util.StringUtil;

import com.hcbd.tdwt.ui.xingyu.activity.AppMapActivity_;


/**
 * - @Description:  选择地图弹框
 * - @Author:  LXJ
 * - @Time:  2018/8/17 9:53
 */

public class ChooseMapPopupWindow implements View.OnClickListener {
    /**
     * 定义结果回调接口
     */
    public interface SendMsgHandler {
        void sendMsg(String map, String longitude, String latitude);
    }

    private SendMsgHandler mSendMsgHandler;
    private Context context;
    private Dialog datePickerDialog;

    private TextView locattionTv;
    private TextView appMapTv;
    private TextView baiduMapTv;
    private TextView gaodeMapTV;
    private TextView tengxunMapTv;
    private TextView systemMapTv;
    private Button cancleBtn;
    private String gpsLongitude;
    private String gpsLatitude;

    public ChooseMapPopupWindow(Context context, String longitude, String latitude, SendMsgHandler sendMsgHandler) {
        this.context = context;
        this.gpsLongitude = longitude;
        this.gpsLatitude = latitude;
        this.mSendMsgHandler = sendMsgHandler;
        initDialog();
    }

    private void initDialog() {
        if (datePickerDialog == null) {
            datePickerDialog = new Dialog(context, R.style.time_dialog);
            datePickerDialog.setCancelable(false);
            datePickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            datePickerDialog.setContentView(R.layout.dialog_choose_map);
            // 设置触摸外部界面使dialog消失
            datePickerDialog.setCanceledOnTouchOutside(true);
            Window window = datePickerDialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = dm.widthPixels;
            window.setAttributes(lp);
            initView();
        }
    }

    private void initView() {
        locattionTv = datePickerDialog.findViewById(R.id.location_tv);
        appMapTv = datePickerDialog.findViewById(R.id.app_map_tv);
        baiduMapTv = datePickerDialog.findViewById(R.id.baidu_map_tv);
        gaodeMapTV = datePickerDialog.findViewById(R.id.gaode_map_tv);
        tengxunMapTv = datePickerDialog.findViewById(R.id.tengxun_map_tv);
        cancleBtn = datePickerDialog.findViewById(R.id.cancel_btn);
        systemMapTv = datePickerDialog.findViewById(R.id.system_map_tv);

        baiduMapTv.setOnClickListener(this);
        appMapTv.setOnClickListener(this);
        gaodeMapTV.setOnClickListener(this);
        tengxunMapTv.setOnClickListener(this);
        cancleBtn.setOnClickListener(this);
        systemMapTv.setOnClickListener(this);
        initDate();
    }

    @SuppressLint("SetTextI18n")
    private void initDate() {
        if(TextUtils.isEmpty(gpsLongitude)||TextUtils.isEmpty(gpsLatitude)){
            locattionTv.setText("经度：" + 0 + "  纬度：" + 0);
        } else if(gpsLongitude.contains("°")){
            locattionTv.setText("经度：" + gpsLongitude + "  纬度：" + gpsLatitude);
        }else{
            locattionTv.setText("经度：" + StringUtil.DDtoDMS_photo(Double.parseDouble(gpsLongitude)) + "  纬度：" + StringUtil.DDtoDMS_photo(Double.parseDouble(gpsLatitude)));
        }

        //百度地图
        if (AppUtil.isAppInstalled(context, "com.baidu.BaiduMap")) {
            baiduMapTv.setVisibility(View.VISIBLE);
        } else {
            baiduMapTv.setVisibility(View.GONE);
        }
        //高德地图
        if (AppUtil.isAppInstalled(context, "com.autonavi.minimap")) {
            gaodeMapTV.setVisibility(View.VISIBLE);
        } else {
            gaodeMapTV.setVisibility(View.GONE);
        }
        //腾讯地图
        if (AppUtil.isAppInstalled(context, "com.tencent.map")) {
            tengxunMapTv.setVisibility(View.VISIBLE);
        } else {
            tengxunMapTv.setVisibility(View.GONE);
        }
    }

    /**
     * 设置经纬度
     *
     * @param longitude
     * @param latitude
     */
    public void setLonLat(String longitude, String latitude) {
        this.gpsLongitude = longitude;
        this.gpsLatitude = latitude;
        initDate();
    }

    public void show() {
        datePickerDialog.show();
    }

    public void hide() {
        datePickerDialog.hide();
    }

    public void dismiss() {
        datePickerDialog.dismiss();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.app_map_tv:
                Intent intent = new Intent(context, AppMapActivity_.class);
                intent.putExtra("lon", gpsLongitude);
                intent.putExtra("lat", gpsLatitude);
                context.startActivity(intent);
                break;
            case R.id.baidu_map_tv:
                mSendMsgHandler.sendMsg("baidu", gpsLongitude, gpsLatitude);
                break;
            case R.id.gaode_map_tv:
                mSendMsgHandler.sendMsg("gaode", gpsLongitude, gpsLatitude);
                break;
            case R.id.tengxun_map_tv:
                mSendMsgHandler.sendMsg("tengxun", gpsLongitude, gpsLatitude);
                break;
            case R.id.cancel_btn:
                hide();
                break;
            case R.id.system_map_tv:
                mSendMsgHandler.sendMsg("othermap", gpsLongitude, gpsLatitude);
                break;
        }
    }
}
